#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
快速RAG测试工具
专门解决超时问题，提供快速配置选项
"""

def quick_test():
    """快速测试，不需要外部依赖"""
    print("🚀 快速RAG系统测试")
    print("=" * 50)
    
    # 模拟检查各种配置对性能的影响
    configs = [
        {
            "name": "原始配置",
            "max_context_files": 5,
            "max_context_length": 4000,
            "similarity_threshold": 0.3,
            "timeout": 60,
            "estimated_time": "2-3分钟"
        },
        {
            "name": "优化配置",
            "max_context_files": 3,
            "max_context_length": 2000, 
            "similarity_threshold": 0.5,
            "timeout": 300,
            "estimated_time": "30-60秒"
        },
        {
            "name": "快速配置",
            "max_context_files": 2,
            "max_context_length": 1000,
            "similarity_threshold": 0.7,
            "timeout": 300,
            "estimated_time": "15-30秒"
        },
        {
            "name": "极速配置", 
            "max_context_files": 1,
            "max_context_length": 500,
            "similarity_threshold": 0.8,
            "timeout": 300,
            "estimated_time": "5-15秒"
        }
    ]
    
    print("📊 不同配置的预期性能:")
    print("-" * 70)
    print(f"{'配置':<12} {'文件数':<8} {'上下文':<8} {'相似度':<8} {'超时':<8} {'预计时间'}")
    print("-" * 70)
    
    for config in configs:
        print(f"{config['name']:<12} {config['max_context_files']:<8} "
              f"{config['max_context_length']:<8} {config['similarity_threshold']:<8} "
              f"{config['timeout']}s{'':<3} {config['estimated_time']}")
    
    print("\n💡 推荐解决方案:")
    print("1. 立即使用: 采用'优化配置'")
    print("2. 如仍超时: 切换到'快速配置'")
    print("3. 紧急情况: 使用'极速配置'")
    
    print("\n🔧 应用优化配置的代码:")
    print("""
from tools.RAGChatBot import RAGChatBot

bot = RAGChatBot(
    ollama_url="http://127.0.0.1:11434",  # 或 http://172.26.32.1:11434
    milvus_collection="massive_file_search"
)

# 应用快速配置
bot.update_config(
    max_context_files=2,
    max_context_length=1000,
    similarity_threshold=0.7
)

if bot.initialize():
    response, sources = bot.chat("有哪些Python文件？")
    print(response)
""")
    
    print("\n🚨 如果仍然超时，检查以下几点:")
    print("1. Ollama服务状态: ollama ps")
    print("2. 系统资源: htop 或任务管理器")
    print("3. 网络连接: ping 127.0.0.1")
    print("4. 模型大小: ollama list (使用更小的模型)")
    
    print("\n🔄 Ollama优化命令:")
    print("export OLLAMA_NUM_PARALLEL=1       # 限制并发")
    print("export OLLAMA_MAX_LOADED_MODELS=1  # 限制加载模型数")
    print("export OLLAMA_FLASH_ATTENTION=1    # 启用Flash Attention")


if __name__ == "__main__":
    quick_test()